13  Data Visualisation - Practical

13.1 Activity: Creating plots using ggplot2

The following exercises are designed to encourage you to become familiar with plotting in ggplot2. They all use datasets that are available from within base R, or within the ggplot2 package.

I’ve provided solutions to each of these exercises. Please remember that R is a hugely flexible language and there are often lots of different ways to achieve the same outcome!

13.1.1 Basic plotting with ggplot2

  • Load the dataset [mtcars].
  • Create a scatter plot of [mpg] vs. [wt].
  • Add a title, and label the axes with meaningful labels.
Show solution
library(ggplot2)

p1 <- ggplot(mtcars, aes(x=wt, y=mpg)) + 
         geom_point() + 
         ggtitle("Miles per Gallon vs. Weight") + 
         xlab("Weight") + 
         ylab("Miles per Gallon")
print(p1)

13.1.2 Layering with geoms

  • Load the dataset [iris].
  • Using the iris dataset, create a scatter plot of [Sepal.Length] vs. [Sepal.Width]. Use different colours for each [Species].
  • Add a smooth line to the plot for each species.
Show solution
p2 <- ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) + 
         geom_point() + 
         geom_smooth(method='lm', se=FALSE)
print(p2)
`geom_smooth()` using formula = 'y ~ x'

13.1.3 Faceting

  • Load the [diamonds] dataset
  • Create a scatter plot of [price] vs. [carat]. Split the data into separate plots by cut using facet_wrap().
  • Adjust the scales to be ‘free’ on the y-axis.
Show solution
p3 <- ggplot(diamonds, aes(x=carat, y=price)) + 
         geom_point() + 
         facet_wrap(~cut, scales="free_y")
print(p3)

13.1.4 Theming

  • Load the dataset [mtcars].
  • Reproduce the scatter plot from exercise 1 (‘Basic plotting with ggplot2’). This time, use the theme_minimal() function to change its appearance.
Show solution
p4 <- ggplot(mtcars, aes(x=wt, y=mpg)) + 
         geom_point() + 
         theme_minimal()
print(p4)

13.1.5 Bar plots and coordinate flips

  • Load the dataset [diamonds].
  • Create a bar plot showing the number of diamonds for each cut.
  • Flip the coordinates to make it a horizontal bar plot.
Show solution
p5 <- ggplot(diamonds, aes(x=cut)) + 
         geom_bar() + 
         coord_flip()
print(p5)

13.1.6 Histograms and binning

  • Load the dataset [diamonds].
  • Create a histogram of diamond prices.
  • Adjust the bin width to change the appearance of the histogram. Try different bin widths to see the effect.
Show solution
p6 <- ggplot(diamonds, aes(x=price)) + 
         geom_histogram(binwidth=500)
print(p6)

13.1.7 Boxplots and outliers

  • Load the dataset [iris].
  • Create a box plot of [Sepal.Length] for each Species.
  • Identify and colour outliers in a different colour.
Show solution
p7 <- ggplot(iris, aes(x=Species, y=Sepal.Length, color=Species)) + 
         geom_boxplot(outlier.color="red")
print(p7)

13.1.8 Position adjustments

  • Load the dataset [mpg].
  • Create a bar plot of number of cars by [class]. Adjust it so the bars are stacked by [drv].
  • Instead of stacking, adjust the position to “dodge” so bars are side by side.
Show solution
p8 <- ggplot(mpg, aes(x=class, fill=drv)) + 
         geom_bar(position="dodge")
print(p8)

13.1.9 Customising legends

  • Load the dataset [mpg].
  • Create a scatter plot of [hwy] vs. [displ] and color points by [class]. Change the legend title to “Car Class”.
  • Adjust the legend position to the bottom of the plot.
Show solution
p9 <- ggplot(mpg, aes(x=displ, y=hwy, color=class)) + 
         geom_point() + 
         labs(color="Car Class") + 
         theme(legend.position="bottom")
print(p9)

13.1.10 Saving your plots

  • Save any of the previous plots to your computer with a resolution suitable for submission as part of an assessment.
  • Try different formats (e.g., PNG, PDF).
Show solution
# Save the first plot as a PNG
ggsave(filename="p1_plot.png", plot=p1, width=6, height=4, dpi=300)

# Save the first plot as a PDF
ggsave(filename="p1_plot.pdf", plot=p1, width=6, height=4)